﻿using AutoMapper;
using Domain.Production_Modules;
using Infratructure.Interface;
using MediatR;
using ReturnValue;
using Yitter.IdGenerator;

namespace MES.Server.API.Application.Command.Production_Modules.GroupInventory
{
    public class AddGroupInventoryHander : IRequestHandler<AddGroupInventoryCommand, ApiResult<int>>
    {
        private readonly IRepository<GroupInventoryModel> groupInventoryRepo;
        private readonly IMapper mapper;
        public AddGroupInventoryHander(IRepository<GroupInventoryModel> groupInventoryRepo, IMapper mapper)
        {
            this.groupInventoryRepo = groupInventoryRepo;
            this.mapper = mapper;
        }

        /// <summary>
        /// 群组添加
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(AddGroupInventoryCommand request, CancellationToken cancellationToken)
        {
            var result = new ApiResult<int>();
            var zhi = mapper.Map<GroupInventoryModel>(request);
            zhi.CreateTime = DateTime.Now;
            zhi.Id = YitIdHelper.NextId();
            if(zhi != null)
            {
                await groupInventoryRepo.AddAsync(zhi);
                result.Code = StatusCode.Success;
                result.Message = "添加群组库存成功";
                return await Task.FromResult(result);
            }
            else
            {
                result.Code = StatusCode.Fail;
                result.Message = "添加群组库存失败";
                return await Task.FromResult(result);
            }
        }
    }
}
